section
  h3(id='error-handling') Error handling

  p.
    Error-handling middleware are defined just like regular middleware,
    however must be defined with an arity of 4, that is the signature
    <code>(err, req, res, next)</code>:

  +js.
    app.use(function(err, req, res, next){
      console.error(err.stack);
      res.send(500, 'Something broke!');
    });

  p.
    Though not mandatory error-handling middleware are typically defined
    very last, below any other <code>app.use()</code> calls as shown here:

  +js.
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(function(err, req, res, next){
      // logic
    });

  p.
    Responses from within these middleware are completely arbitrary. You may
    wish to respond with an HTML error page, a simple message, a JSON string,
    or anything else you prefer.

  p.
    For organizational, and higher-level framework purposes you may define
    several of these error-handling middleware, much like you would with
    regular middleware. For example suppose you wanted to define an error-handler
    for requests made via XHR, and those without, you might do:

  +js.
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(logErrors);
    app.use(clientErrorHandler);
    app.use(errorHandler);

  p.
    Where the more generic <code>logErrors</code> may write request and
    error information to stderr, loggly, or similar services:

  +js.
    function logErrors(err, req, res, next) {
      console.error(err.stack);
      next(err);
    }

  p.
    Where <code>clientErrorHandler</code> is defined as the following, note
    that the error is explicitly passed along to the next.

  +js.
    function clientErrorHandler(err, req, res, next) {
      if (req.xhr) {
        res.send(500, { error: 'Something blew up!' });
      } else {
        next(err);
      }
    }

  p.
    The following <code>errorHandler</code> "catch-all" implementation may be defined as:

  +js.
    function errorHandler(err, req, res, next) {
      res.status(500);
      res.render('error', { error: err });
    }